<?php

/**
 * Class ApiService
 */
class AdminService extends BaseService
{
    private static $self = NULL;

    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }

        return self::$self;
    }

    private $status_msgs = NULL;
    private $mAdmin = NULL;
    private $mAds = NULL;
    private $mLog = NULL;
    private $mProject = NULL;
    private $mInvestRecord = NULL;
    private $mTicketRecord = NULL;
    private $mRechargeRecord = NULL;
    private $mWithdrawRecord = NULL;
    private $mBankManage = NULL;
    private $mUser = NULL;
    private $mNews = NULL;
    private $mAbutPartner = NULL;
    private $mAbutTeam = NULL;
    private $mAbutRecruit = NULL;
    private $mCompany = NULL;
    private $mUserExtend = NULL;
    private $mInvitie = NULL;
    private $mMessage = NULL;
    private $mLookUser = NULL;
    private $mLockLogin = NULL;
    private $mBusinessPic = NULL;
    private $mExperRecord = NULL;
    private $mExperScale = NULL;
    private $mLookWithdraw = NULL;
    private $mLineChart = NULL;


    public function __construct()
    {
        $this->status_msgs = ProfessionErrorCodeEnum::$status_msgs;
        $this->mAdmin = new Involved_AdminModel();
        $this->mAds = new Involved_AdsModel();
        $this->mLog = new Involved_LogModel();
        $this->mProject = new Involved_ProjectModel();
        $this->mInvestRecord = new Involved_InvestRecordModel();
        $this->mTicketRecord = new Involved_TicketRecordModel();
        $this->mRechargeRecord = new Involved_RechargeRecordModel();
        $this->mWithdrawRecord = new Involved_WithdrawRecordModel();
        $this->mBankManage = new Involved_BankManageModel();
        $this->mUser = new Involved_UserModel();
        $this->mNews = new Involved_NewsModel();
        $this->mAbutPartner = new Involved_AbutPartnerModel();
        $this->mAbutTeam = new Involved_AbutTeamModel();
        $this->mAbutRecruit = new Involved_AbutRecruitModel();
        $this->mCompany = new Involved_CompanyModel();
        $this->mUserExtend = new Involved_UserExtendModel();
        $this->mInvitie = new Involved_InvitieModel();
        $this->mMessage = new Involved_MessageModel();
        $this->mLookUser = new Involved_LookUserModel();
        $this->mLockLogin = new Involved_LockLoginModel();
        $this->mBusinessPic = new Involved_BusinessPicModel();
        $this->mExperRecord = new Involved_ExperRecordModel();
        $this->mExperScale = new Involved_ExperScaleModel();
        $this->mLookWithdraw = new Involved_LookWithdrawModel();
        $this->mLineChart = new Involved_LineChartModel();

    }

    /**
     * 参数验证
     * @param $obj
     * @param $arr
     * @author alice
     * @date 2016-1-13
     * @return bool
     * @return bool
     */
    public function  checkProperties($obj, $arr)
    {
        if (empty($arr)) return true;
        $obj = (array)$obj;

        foreach ($arr as $v) {
            if (is_null($obj[$v])) {
                return false;
            }
        }
        return true;
    }

    /**
     * 随机数
     * @param $length
     * @return string
     */
    function random_str($length)
    {
        //生成一个包含 大写英文字母, 小写英文字母, 数字 的数组
        $arr = array_merge(range(0, 9), range('a', 'z'));

        $str = '';
        $arr_len = count($arr);
        for ($i = 0; $i < $length; $i++)
        {
            $rand = mt_rand(0, $arr_len-1);
            $str.=$arr[$rand];
        }

        return $str;
    }

    /**
     * 获取体验金
     * @param $user_id
     * @return mixed
     */
   public function GetCash($user_id)
   {
       return $this->mUser->ApiGetUserInfo($user_id);

   }

    /**
     * 手机验证
     * @param $paramObj
     * @return mixed
     */
    public function CheckMobile($paramObj)
    {

        #手机号是否已注册
        $mobile =  $this->mUser->JsCheckMobile($paramObj->mobile);
        return $mobile;
    }

    /**
     * 发送短信验证
     * @param $paramObj
     * @return string
     */
    public function VerifyNews($paramObj)
    {
        $info =session_get(UserEnum::CLIENT_NEWS_CAPTCHA);

        if(time()-$info['time']<60&&$paramObj->mobile==$info['mobile']){
            return 10001;
        }else{
            #接受参数
            $aData["mobile"] = $paramObj->mobile;
            #生成随机验证码
            $news_captcha = substr(str_shuffle("012345678901234567890123456789"), 0, 6);
            #发送参数
            $data = $this->mLog->sendTemplateSMS($paramObj->mobile,array($news_captcha,'10'),94059);
            if($data == 10000){
                $aData['time'] = time();
                $aData['news_captcha'] = $news_captcha;
                Session::put(UserEnum::CLIENT_NEWS_CAPTCHA,$aData);
                return 10000;
            }elseif($data == 160040){
                return 160040;
            }

        }
    }

    /**
     * 短信验证
     * @param $paramObj
     * @return int
     */
    public function CheckNews($paramObj)
    {
        $info =session_get(UserEnum::CLIENT_NEWS_CAPTCHA);
        $time =time()-$info['time'];
        if($info['news_captcha'] == $paramObj->news_captcha && $info['mobile'] == $paramObj->mobile){
            if($time<=600){
                return 1;
            }else{
                return 2;
            }
        }else{
            return  3;
        }
    }
    /**
     * 注册验证
     * @param $paramObj
     * @return int
     * @throws Exception
     */
    public function VerifyRegister($paramObj)
    {
        #参数验证
        if(empty($paramObj->pwd) || empty($paramObj->mobile) || empty($paramObj->news_captcha)){
            throw new Exception('必填项不能为空', ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        #手机号验证
        if (!preg_match('/^1[34578]\d{9}$/', $paramObj->mobile)) {
            throw new Exception("手机号码错误", ProfessionErrorCodeEnum::ERROR_USER_MOBILE_FAILURE);
        }

        #手机号是否已注册
        $mobile = $this->mUser->JsCheckMobile($paramObj->mobile);
        if ($mobile) {
            throw new Exception("手机号已注册", ProfessionErrorCodeEnum::ERROR_ACCOUNT_EXIST);
        }

        #判断短信验证
        $info =session_get(UserEnum::CLIENT_NEWS_CAPTCHA);
        if ($info['news_captcha']!=$paramObj->news_captcha) {
            throw new Exception("短信验证码有误", ProfessionErrorCodeEnum::ERROR_ACCOUNT_EXIST);
        }


        #注册用户
        $aData = [
            'password'           =>     md5($paramObj->pwd),
            'mobile'             =>     $paramObj->mobile,
            'avatar'             =>     'api.yangpiaotong.com/resource/img/user_img.png',
            'user_name'          =>     'ypt_'.$this->random_str(9),
            'created'            =>     time()
        ];

        $user_id = $this->mUser->insert($aData);

        if($user_id){
            if(!empty($paramObj->invite_mobile)){
                $invite = $this->mUser->JsCheckInvite($paramObj->invite_mobile);
                if($invite){
                    $aData2 = [
                        'bu_id'      => $user_id,
                        'u_id'      =>  $invite->user_id,
                        'reward'     =>  '100',
                        'datetime'   =>  '30',
                        'time'       =>  time(),
                        'access'     =>  '注册获取',
                        'statue'     =>  1,
                    ];
                    $this->mInvitie->insert($aData2);
                }
            }else{
                if(!empty($paramObj->invite_id)){
                    $aData2 = [
                        'bu_id'      =>  $user_id,
                        'u_id'       =>   $paramObj->invite_id,
                        'reward'     =>  '100',
                        'datetime'   =>  '30',
                        'time'       =>  time(),
                        'access'     =>  '注册获取',
                        'statue'     =>  0,
                    ];
                    $this->mInvitie->insert($aData2);
                }else{
                    $aData2 = [
                        'u_id'     =>  $user_id,
                        'reward'    =>  '100',
                        'datetime'   =>  '30',
                        'time'      =>  time(),
                        'access'    =>  '注册获取',
                        'statue'    =>  0,
                    ];
                    $this->mInvitie->insert($aData2);
                }
            }
            $aData3=[
                'scale'      =>  '10000',
                'time'       =>  time(),
                'datetime'   =>  '30',
                'access'     =>  '注册获取',
                'user_id'    =>  $user_id,
                'statue'     =>   0,
            ];

            $this->mExperScale->insert($aData3);
            $this->mUserExtend->insert(['user_id'=>$user_id]);
        }else{
            throw new Exception('注册失败');
        }

    }
}